package com.fc.service.impl;

import org.hibernate.Criteria
import org.hibernate.criterion.Restrictions
import org.springframework.stereotype.Repository
import org.springframework.transaction.annotation.Transactional

import com.fc.entities.Recipe
import com.fc.entities.User
import com.fc.service.RecipeService
import com.fc.service.exceptions.FCException

/**
 * 
 * @author Ács Ádám
 *
 */
@Repository("recipeService")
@Transactional
class RecipeServiceImpl extends BaseServiceImpl<Recipe> implements RecipeService  {

	@Override
	Recipe findById(long id) {
		find(Recipe.class, id)
	}

	@Override
	List<Recipe> findAll() {
		findAll(Recipe.class)
	}

	@Override
	void delete(Recipe t) {
		try {
			super.delete(t);
		} catch (Exception ex) {
			throw new FCException("Az adott recept (${t.name}) törlése nem lehetséges.")
		}
	}
	
	@Override
	void delete(long id) {
		try {
			super.delete(id);
		} catch (Exception ex) {
			throw new FCException("Az adott recept törlése nem lehetséges.")
		}
	}

	@Override
	List<Recipe> findAllByUser(User user) {
		Criteria criteria = currentSession().createCriteria(Recipe.class)
		return criteria.add(Restrictions.eq("user", user)).list()
	}
}
